package com.sun.rabbit.topics.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.sun.rabbit.utils.RabbitMqUtils;

/**消费者
 * @author shkstart
 * @create 2021-10-16 21:43
 */
public class ReceiveLogsTopic02 {
    private  static  final String EXCHANGE_NAME = "topic_logs" ;

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        String QUEUE_NAME="Q2";
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"*.*.rabbit");
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"lazy.#");
        System.out.println(" 等待接收消息.....");
        DeliverCallback deliverCallback=(consumerTag, delivery) ->{
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" 接 收 队 列 :"+QUEUE_NAME+" 绑 定 键:"+delivery.getEnvelope().getRoutingKey()+", 消息:"+message);
        };
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,consumerTag -> {System.out.println("消息中断");});
    }
}
